package cn.tedu._13weibo626.base.exception;

import cn.tedu._13weibo626.base.resopnse.JsonResult;
import cn.tedu._13weibo626.base.resopnse.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        String data = ex.getFieldError().getDefaultMessage();
        log.error("doHandleMethodArgumentNotValidException:" + data);
        return new JsonResult(StatusCode.VALIDATED_ERROR, data);
    }
    @ExceptionHandler
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException ex) {
        String data = ex.getMessage().split(":")[1].trim();
        log.debug("ConstraintViolationException:",data);
        return new JsonResult(StatusCode.VALIDATED_ERROR, data);
    }
}
